<?php

namespace App\Admin\Forms;


use App\Models\ActivityInformation;
use App\Models\Contract;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\Storage;
use lastguest\Murmur;
use zgldh\QiniuStorage\QiniuStorage;

class EntryInformation extends Form implements LazyRenderable
{
    use LazyWidget;

    public function handle(array $input)
    {
        // $input['url'] = config('filesystems.disks.qiniu.domains.https') . $input['url'];
        $resutl = ActivityInformation::updateOrCreate(['act_id' => $input['act_id']], $input);
        if ($resutl) {
            return $this->response()->success('修改成功')->refresh();
        } else {
            return $this->response()->error('修改失败')->refresh();
        }
    }

    public function
    default()
    {

        return [
            // 展示上个页面传递过来的值
            'act_id' => $this->payload['act_id'],
            'cover'       => isset($this->payload['cover']) ? $this->payload['cover'] : '',
            'video'       => isset($this->payload['video']) ? $this->payload['video'] : '',
            'activity_theme' => isset($this->payload['activity_theme']) ? $this->payload['activity_theme'] : '',
            'medical_template'   => isset($this->payload['medical_template']) ? $this->payload['medical_template'] : '',
            'entry_process_pc'    => isset($this->payload['entry_process_pc']) ? $this->payload['entry_process_pc'] : '',
            'entry_process_mobile'    => isset($this->payload['entry_process_mobile']) ? $this->payload['entry_process_mobile'] : '',
            'entry_information'   => isset($this->payload['entry_information']) ? $this->payload['entry_information'] : '',
            // 'expert_judge'   => isset($this->payload['expert_judge']) ? $this->payload['expert_judge'] : '',
        ];
    }

    public function form()
    {
        $filename = md5(uniqid()) . '.mp4';
        $disk = QiniuStorage::disk('qiniu');
        // 回调信息, Dcat 上传文件需要的报文格式
        $callBack = [
            'status' => true,
            'data'   => [
                'id'   => $filename,
                'name' => $filename,
                'path' => $filename,
                'url'  => env('QINIU_DOMAINS') . '/' . $filename,
            ],
        ];
        $policy   = [
            'returnBody' => json_encode($callBack),
        ];
        // 七牛云上传 token
        $token    = $disk->uploadToken($filename, 3600, $policy);
        $this->hidden('act_id');
        $this->image('cover', '视频封面图')->autoUpload()->move('medical/innovent/cover/' . date('Y/m'));
        // $this->file('video', '赛区宣传视频')->disk('public')->autoUpload()->move('medical/innovent/video/' . date('Y/m'))->maxSize(1024 * 1024)->chunked()->chunkSize(1024 * 3)->threads(8);
        // $this->file('video', '赛区宣传视频')->autoUpload()->move('medical/innovent/video/' . date('Y/m'))->maxSize(1024 * 1024)->chunked()->chunkSize(1024 * 3)->threads(8);
        $this->file('video', '赛区宣传视频')
            ->disk('qiniu')
            ->autoUpload()
            ->accept('mp4')
            // 提示文字
            ->help('仅支持MP4格式')
            // ->move('medical/innovent/video/' . date('Y/m'))
            ->maxSize(1024 * 1024)
            // 关闭页面删除
            ->removable(false)
            // ->chunked()
            // ->chunkSize(1024 * 3)
            // ->threads(8) // 设置上传地址
            ->url('https://up-z1.qiniup.com')
            // 自定义 webuploader 配置
            // Dcat 默认 name 为 _file_
            ->options(['fileVal' => 'file'])
            // 上传事件
            ->on(
                'startUpload',
                <<<JS
        function () {
            console.log('文件开始上传...', this);
            // 上传文件前附加自定义参数到文件上传接口
            this.uploader.options.formData['token'] = "$token";
            this.uploader.options.formData['fileName'] = "$filename";
            this.uploader.options.formData['key'] = "$filename";
        }
JS
            );
        $this->textarea('activity_theme', '活动主旨');
        $this->file('medical_template', '病例模板')->autoUpload()->move('medical/innovent/medical_template/' . date('Y/m'));
        $this->image('entry_process_pc', '参赛流程PC端')->autoUpload()->move('medical/innovent/entry_process_pc/' . date('Y/m'));
        $this->image('entry_process_mobile', '参赛流程移动端')->autoUpload()->move('medical/innovent/entry_process_mobile/' . date('Y/m'));
        $this->editor('entry_information', '参赛信息');
        // $this->array('expert_judge', '专家评委', function ($table) {
        //     $table->image('avatar', '头像')->autoUpload()->move('medical/innovent/avatar/' . date('Y/m'))->saveFullUrl();
        //     $table->text('name', '专家名称');
        // $table->textarea('desc');
        // });
    }
}
